package IO;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import MetroMessenger.SocketMessengerConstants;

public class File 
{
	public static boolean authenticate(String login,String password) throws IOException
	{	
		boolean flag=false;
		String fileName = "database.txt",line,user,pass;
		BufferedReader reader = new BufferedReader(new FileReader(fileName));		 
		while ((line = reader.readLine()) != null) 
		{
			StringTokenizer token=new StringTokenizer(line,SocketMessengerConstants.MESSAGE_SEPARATOR);	
			user=token.nextToken();
			pass=token.nextToken();
			if(user.equals(login))
			{
				if(pass.equals(password))
				{
					flag=true;
					return flag;
				}
			}				
		}
		return flag;		
	}
	public void writeFile(String login,String password) 
	{
		String fileName = "database.txt";
		 
		try 
		{
		PrintWriter writer = new PrintWriter(new FileWriter(fileName,true));
		writer.println(login+SocketMessengerConstants.MESSAGE_SEPARATOR+password);
		writer.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
	}
}
